package com.koala.learn.multhread;

import cn.hutool.crypto.digest.MD5;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author yantingrui
 * @date 2022/2/25   14:40
 */
@Data
@Slf4j
public class Account {
    private int balance;
    private final Lock lock = new ReentrantLock();

    public void transfer(Account target, int amt) {
        //synchronized (Account.class){
        //    if(this.balance >= amt ){
        //        this.balance -= amt;
        //        target.balance += amt;
        //    }
        //}

    }

    public static void main(String[] args) {
        MD5 md5 = MD5.create();
        String s = md5.digestHex16("123");
        System.out.println(s);

    }

    private static String getUrl(HashMap<String, String> params) {
        String url = "";
        // 添加url参数
        if (params != null) {
            Iterator<String> it = params.keySet().iterator();
            StringBuffer sb = null;
            while (it.hasNext()) {
                String key = it.next();
                String value = params.get(key);
                if (sb == null) {
                    sb = new StringBuffer();
                    sb.append("?");
                } else {
                    sb.append("&");
                }
                sb.append(key);
                sb.append("=");
                sb.append(value);
            }
            url += sb.toString();
        }
        return url;
    }

    public Account(int balance) {
        this.balance = balance;
    }

    //public static void main(String[] args) throws InterruptedException {
    //Account account1 = new Account(1000);
    //Account account2 = new Account(0);
    //Thread[] t = new Thread[10];
    //for(int i = 0;i < 10;i++){
    //    t[i] = new Thread(()->{
    //        account1.transfer(account2,10);
    //    });
    //}
    //for(int i = 0;i < 10;i++){
    //    t[i].start();
    //    System.out.println(t[i].getState());
    //}
    //
    //Thread.sleep(1000);
    //log.info("转账 完成后 A的剩余{} B的剩余{}",account1.getBalance(),account2.getBalance());
    //}
}
